package com.yilijk.base.oss;

import android.annotation.SuppressLint;
import android.content.Context;

import com.google.gson.Gson;
import com.yilijk.base.network.RequestParams;
import com.yilijk.base.network.config.BaseConfig;
import com.yilijk.base.network.https.HttpCallBack;
import com.yilijk.base.network.https.HttpUtils;
import com.yilijk.base.network.https.interceptor.NetWorkUtils;
import com.yilijk.base.oss.bean.OssConfigBean;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.DevicesUtils;

import org.json.JSONException;
import org.json.JSONObject;

@SuppressLint("LongLogTag")
public class UploadOssUtils {

    public OssConfigBean.DataBean data;

    private static UploadOssUtils uploadOssUtils;

    public static  synchronized UploadOssUtils getInstance(){
        if (uploadOssUtils == null){
            uploadOssUtils = new UploadOssUtils();
        }
        return uploadOssUtils;
    }

    public void init(){
        getOssInfo();
    }




    private void getOssInfo(){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("scene", "cobra");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestParams params = new RequestParams();
        params.put("scene","cobra");
        HttpUtils.get(BaseConfig.Url.OssUrl, params, new HttpCallBack<OssConfigBean>() {
            @Override
            public void onSuccess(OssConfigBean ossConfigBean, int code) {
                if (ossConfigBean.getCode() == 200){
                    ALog.i("Class:UploadOssUtils -- Method: getOssInfo()",ossConfigBean.getMessage());
                    data = ossConfigBean.getData();
                }
            }

            @Override
            public void onFailure(String failureMsg, int statusCode) {

            }
        });
    }



    public JSONObject getUpLoadParams(String type,String filename){
        JSONObject jsonObject = new JSONObject();;
        if (data==null){
            return null;
        }else{
            try {
                jsonObject.put("policy", data.getPolicy());
                jsonObject.put("OSSAccessKeyId", data.getAccessKeyId());
                jsonObject.put("signature", data.getSignature());
                jsonObject.put("key", data.getStartWith()+type+filename);
                jsonObject.put("success_action_status", "200");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jsonObject;
    }



    public OssConfigBean.DataBean getData() {
        return data;
    }

    public void setData(OssConfigBean.DataBean data) {
        this.data = data;
    }

}
